package com.ttbj.product.slideshow.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.slideshow.SlideShowDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.slideshow.SlideshowService;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.annotation.SystemServiceLog;
import com.ttbj.slideshow.dao.SlideShowMapper;

@Service("slideshowService")
public class SlideShowServiceImpl implements SlideshowService{
	
	@Autowired
	private SlideShowMapper slideShowMapper;

	@Override
	public List<SlideShowDto> findAllSlideShowInfoPage(Page page) throws BusinessException {
		List<SlideShowDto> slideShowDtos = this.slideShowMapper.queryAllSlideShowInfoPage(page);
		return slideShowDtos;
	}

	@Override
	public Integer findAllSlideShowInfoPageCount() throws BusinessException {
		int count = this.slideShowMapper.queryAllSlideShowInfoPageCount();
		return count;
	}
	
	@Override
	public List<SlideShowDto> findAAllSlideShowOrder() throws BusinessException {
		List<SlideShowDto> slideShowDtos = this.slideShowMapper.queryAllSlideShowOrder();
		return slideShowDtos;
	}

	@Override
	public SlideShowDto findSlideShowInfoById(Integer slideId) throws BusinessException {
		if (null == slideId) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		SlideShowDto slideShowDto = this.slideShowMapper.querySlideShowInfoById(slideId);
		return slideShowDto;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="增加幻灯片信息")
	public void addSlideShowInfo(SlideShowDto showDto) throws BusinessException {
		if (StringUtils.isBlank(showDto.getSlideImgName()) && showDto.getSlideOrder() < 0) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		showDto.setSlideInsTime(new Date());
		int result = this.slideShowMapper.inserSlideShowInfo(showDto);
		if (result == 0) {
			throw new BusinessException("未知错误，添加失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}

	@Override
	@Transactional
	@SystemServiceLog(description="修改幻灯片信息")
	public void editSlideShowInfoById(SlideShowDto showDto) throws BusinessException {
		if (StringUtils.isBlank(showDto.getSlideImgName()) && showDto.getSlideOrder() > 0) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		showDto.setSlideUpdateTime(new Date());
		int result = this.slideShowMapper.updateSlideShowInfo(showDto);
		if (result == 0) {
			throw new BusinessException("未知错误，修改失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}
	
	@Override
	@Transactional
	@SystemServiceLog(description="删除幻灯片信息")
	public void delSlideShowInfoById(SlideShowDto showDto) throws BusinessException {
		if (null == showDto.getSlideId()) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		int result = this.slideShowMapper.deleteSlideShowInfoById(showDto);
		if (result == 0) {
			throw new BusinessException("未知错误，删除失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}
}
